package com.atguigu.chapter07.D02_WaterMark;

import com.atguigu.bean.WaterSensor;
import com.atguigu.util.AnqclnUtil;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;
import org.apache.flink.util.OutputTag;

import java.time.Duration;
import java.util.List;

/**
 * Author: Pepsi
 * Date: 2023/8/7
 * Desc:
 *      侧输出流的第二个作用，数据的分流
 *      如果想使用侧输出流，必须使用process，因为process里面才有context,它才能output
 *
 */
public class Flink06_Sideout_2 {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port",1000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);

        env.setParallelism(1);

        // 设置水印生成的周期   水印两秒生成一次
        env.getConfig().setAutoWatermarkInterval(2000);

        SingleOutputStreamOperator<String> main = env
                .socketTextStream("hadoop101", 9999)
                .map(line -> {
                    String[] data = line.split(",");
                    return new WaterSensor(
                            data[0],
                            Long.valueOf(data[1]),
                            Integer.valueOf(data[2])
                    );
                })
                // 用侧输出流的话，必须用process,它里面有上下文，可以将数据放到侧输出流里面
                .process(new ProcessFunction<WaterSensor, String>() {
                    @Override
                    public void processElement(WaterSensor value,
                                               Context ctx,
                                               Collector<String> out) throws Exception {
                        if ("s1".equals(value.getId())) {
                            out.collect(value.toString());
                        } else if ("s2".equals(value.getId())) {
                            ctx.output(new OutputTag<String>("s2") {}, value.toString());
                        } else {
                            ctx.output(new OutputTag<String>("other"){}, value.toString());
                        }
                    }
                });


        main.print("main Stream >");
        main.getSideOutput(new OutputTag<String>("s2"){}).print("sideOutPut_s2");
        main.getSideOutput(new OutputTag<String>("other"){}).print("sideOutPut_other");

        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
